import { mock, Random } from 'mockjs'
import { defineMock } from '@alova/mock'
import { CustomErrorCode, CustomSuccessCode } from '@/api/enums'

import type {
  GetVerifyCodeResponseData,
  LoginResponseData,
  SendPhoneVerifyCodeResponseData
} from '@/api/user/types'

export default defineMock({
  '[POST]/auth/login'(): LoginResponseData {
    return Math.random() > 0.5
      ? {
          code: CustomSuccessCode.OK,
          message: '登录成功',
          data: mock({
            access_token: Random.string('lower', 32, 32)
          })
        }
      : {
          code: CustomErrorCode.BAD_REQUEST,
          message: '用户名/密码错误',
          data: mock({
            uuid: Random.string('lower', 32, 32),
            verifycode: Random.image('100x50', '#0000', '#000', 'png', '12ab')
          })
        }
  },
  '[GET]/verify-code/{id}'(): GetVerifyCodeResponseData {
    return Math.random() > 0.5
      ? {
          code: CustomErrorCode.VERIFY_CODE_FREQUENT,
          message: '验证码请求过于频繁',
          data: {}
        }
      : {
          code: CustomSuccessCode.OK,
          message: '获取验证码成功',
          data: {
            uuid: Random.string('lower', 32, 32),
            verifyCode: Random.image('100x50', '#0000', '#000', 'png', '12ab')
          }
        }
  },
  '[GET]/verify-code/phone/{id}'(): SendPhoneVerifyCodeResponseData {
    return Math.random() > 0.5
      ? {
          code: CustomErrorCode.VERIFY_CODE_FREQUENT,
          message: '验证码请求过于频繁',
          data: {}
        }
      : {
          code: CustomSuccessCode.OK,
          message: '已发送验证码',
          data: {}
        }
  },
  '[POST]/user/register'() {
    return Math.random() > 0.5
      ? {
          code: CustomSuccessCode.OK,
          message: '注册成功',
          data: mock({ access_token: Random.string('lower', 32, 32) })
        }
      : {
          code: CustomErrorCode.BAD_REQUEST,
          message: '用户名已存在',
          data: {}
        }
  }
})
